import { jsx as _jsx } from "react/jsx-runtime";
import { Col, Form, Row } from 'antd';
import { forwardRef, useImperativeHandle } from 'react';
import FormNode from '../FormNode';
const FormItem = Form.Item;
const keys = [
    'type',
    'placeholder',
    'options',
    'required',
    'message',
    'disabled',
    'allowClear',
    'tree',
    'treeData',
    'min',
    'max',
    'addonAfter',
    'showSearch',
    'onSearch'
];
const initLayout = {
    labelCol: { span: 6 },
    wrapperCol: { span: 17 }
};
const CustomForm = forwardRef((props, ref) => {
    const { configs = [], initialValues = {}, layout = initLayout, onChange } = props;
    const [form] = Form.useForm();
    useImperativeHandle(ref, () => {
        return {
            validateFields: form.validateFields
        };
    });
    const valuesChange = (_values, allValues) => {
        onChange && onChange(allValues);
    };
    return (_jsx(Form, { form: form, initialValues: initialValues, onValuesChange: valuesChange, children: _jsx(Row, { children: configs.map((item, idx) => {
                const data = {};
                keys.forEach((i) => {
                    if (![null, undefined].includes(item[i])) {
                        data[i] = item[i];
                    }
                });
                if (!item.field) {
                    return (_jsx(Col, { span: item.span || 24, children: _jsx(FormItem, { label: item.label, style: { fontSize: '15px' } }) }, idx + item.label));
                }
                return (_jsx(Col, { span: item.span || 24, children: _jsx(FormItem, { name: item.field, label: item.label, rules: [{ required: item.required, message: item.message }], ...layout, children: _jsx(FormNode, { ...data }) }) }, item.field));
            }) }) }));
});
export default CustomForm;
